#include "Login.h"
#include <vector>
#include <sstream>
void split(const string &s, char delim, vector<string> &tok)
{
    stringstream ss(s);
    string item;
    while (getline(ss, item, delim))
    {
        tok.push_back(item);
    }
}

istream & operator>>(istream &in, Login  &l)
{
    string str;
    in >> str;
    vector<string> toks ;
    char c=':';
    split(str,c,toks);
    if (3 == toks.size())
    {
        l.m_strID = toks.at(0);
        l.m_strPassword  =  toks.at(1);
        l.m_strAccountStatus = toks.at(2);
    }
    return in;
}
ostream & operator<<(ostream &out, Login  &l)
{
    char c=':';
    string str;
    out<<  l.m_strID << c << l.m_strPassword << c << l.m_strAccountStatus;
    return out;
}
LoginSystem::LoginSystem(string file):m_strLoginFile(file)
{
}
bool LoginSystem::LoadLoginFile()
{
    bool bStat = false;

    ifstream infile;

    infile.open(m_strLoginFile.c_str(),std::ifstream::in);

    if(infile.fail ())
    {
        return false;
    }

    Login login;
    while(infile >> login) {
        cout <<login<<endl;
        m_vecLogin.push_back(login);
    }

    return bStat;
}

bool LoginSystem::LoadLoginFile()
{
    bool bStat = false;

    ifstream infile;

    infile.open(m_strLoginFile.c_str(),std::ifstream::in);

    if(infile.fail ())
    {
        return false;
    }

    Login login;
    while(infile >> login) {
        cout <<login<<endl;
        m_vecLogin.push_back(login);
    }

    return bStat;
}



bool
LoginSystem :: LoginUser()
{
    bool bStat = false;
    int iRetryCnt = 0;
    do
    {
        string id,pass;
        cout <<"Enter User ID:";
        cin >>id;
        cout <<"Enter Password :";
        cin >>pass;
        ++iRetryCnt;
    } while(iRetryCnt<=3);

}
